package com.zitronella.course.osseventmanager.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.apache.commons.lang.StringUtils;
import com.zitronella.course.osseventmanager.domain.Event;
import com.zitronella.course.osseventmanager.domain.EventUser;
import com.zitronella.course.osseventmanager.service.EventService;
import com.sun.deploy.net.HttpRequest;

import javax.servlet.http.HttpSession;
import java.text.ParseException;

/**
 * Created by IntelliJ IDEA.
 * User: Robin
 * Date: Mar 24, 2009
 * Time: 9:31:32 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class EventDetailController {

     private EventService eventService;

    @Autowired
    public void setEventService(EventService evService) {
        this.eventService = evService;
    }

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields(new String[] {"id"});
    }

    @RequestMapping(value = "/event/detail", method = RequestMethod.GET)
    public String setupForm(Model model , @RequestParam(required=true) String eventName, HttpSession session) {
       
         model.addAttribute("event", eventService.findEventByName(eventName));
        return "event";
    }


    @RequestMapping(value = "/event/detail", method = RequestMethod.POST)
    public String saveEvent(@ModelAttribute Event event, BindingResult result, HttpSession session) throws ParseException {

        if (!result.hasErrors()) {

            if (event != null) {
                session.setAttribute("event", event);

                System.out.println("Description" + event.getName());
                Event dbEvent = eventService.findEventByName(event.getName());
                dbEvent.setDescription(event.getDescription());
                dbEvent.setLocation(event.getLocation());
                dbEvent.setName(event.getName());
                
                System.out.println("Description" +dbEvent.getDescription() +  "Id" +dbEvent.getId() + "Location" + dbEvent.getLocation() + "name" + dbEvent.getName());

                eventService.updateEvent(dbEvent);
                return "redirect:/controller/myevents";
            }
            return "redirect:/controller/events";
        } else {
            return "redirect:/controller/events";
        }
    }

    private EventUser getEventUser(HttpSession session) {
        EventUser testUser = new EventUser("robin@gmail.com","robin", "","");
        session.setAttribute("user",testUser);
        return (EventUser) session.getAttribute("user");
    }
    private Event getEvent(HttpSession session) {
        //simulating an existing event to test 
        Event e = new Event("Systems Engineering1","Robin", "Thinking Event","Minneapolis", null, null);
        session.setAttribute("eventFromMyEvent",e);
                      //eventService.findEvent();
//        /eventService.updateEvent(e);
        
        return (Event) session.getAttribute("e");
    }

    private long getEventId(HttpSession session) {
     Long id = (Long) session.getAttribute("eventId");
        return id.longValue();
    }

    private String getEventName(HttpSession session) {
     String name = (String) session.getAttribute("eventName");
        return name;
    }

}
